// 课程数据
const courses = [
    {
        id: 1,
        code: "MATH101",
        name: "高等数学",
        teacher: "李教授",
        type: "必修",
        credits: 4,
        time: "星期一 08:00-09:40 (第1-2节)",
        location: "理教201",
        weeks: "第1-16周",
        description: "本课程是高等数学的基础课程，主要内容包括函数、极限、导数、积分等基本概念和方法。通过本课程的学习，学生将掌握高等数学的基本理论和方法，为后续专业课程的学习打下坚实的数学基础。",
        syllabus: [
            "第1章：函数与极限",
            "第2章：导数与微分",
            "第3章：微分中值定理与导数的应用",
            "第4章：不定积分",
            "第5章：定积分",
            "第6章：定积分的应用"
        ]
    },
    {
        id: 2,
        code: "PHYS101",
        name: "大学物理",
        teacher: "王教授",
        type: "必修",
        credits: 3,
        time: "星期二 08:00-09:40 (第1-2节)",
        location: "理教302",
        weeks: "第3-16周",
        description: "本课程是大学物理的基础课程，主要内容包括力学、热学、电磁学等基本概念和规律。通过本课程的学习，学生将掌握大学物理的基本理论和方法，培养科学思维和解决实际问题的能力。",
        syllabus: [
            "第1章：质点运动学",
            "第2章：牛顿运动定律",
            "第3章：机械能守恒定律",
            "第4章：刚体的转动",
            "第5章：热力学基础",
            "第6章：静电场"
        ]
    },
    // 其他课程数据省略...
];

// DOM 元素
const gridViewBtn = document.getElementById('gridViewBtn');
const listViewBtn = document.getElementById('listViewBtn');
const gridView = document.getElementById('gridView');
const listView = document.getElementById('listView');
const courseModal = document.getElementById('courseModal');
const closeModalBtn = document.getElementById('closeModalBtn');
const cancelModalBtn = document.getElementById('cancelModalBtn');
const prevWeekBtn = document.getElementById('prevWeekBtn');
const nextWeekBtn = document.getElementById('nextWeekBtn');
const currentWeekEl = document.getElementById('currentWeek');

// 当前选中的周
let currentWeek = 12;

// 初始化日期显示
function updateWeekDisplay() {
    const startDate = new Date(2025, 4, 11); // 2025-05-11
    startDate.setDate(startDate.getDate() + (currentWeek - 12) * 7);
    
    const endDate = new Date(startDate);
    endDate.setDate(endDate.getDate() + 6);
    
    const formatOptions = { year: 'numeric', month: '2-digit', day: '2-digit' };
    const startDateStr = startDate.toLocaleDateString('zh-CN', formatOptions);
    const endDateStr = endDate.toLocaleDateString('zh-CN', formatOptions);
    
    currentWeekEl.textContent = `第${currentWeek}周 (${startDateStr}至${endDateStr})`;
}

// 切换视图
gridViewBtn.addEventListener('click', () => {
    gridView.classList.remove('hidden');
    listView.classList.add('hidden');
    gridViewBtn.classList.add('bg-blue-600', 'text-white');
    gridViewBtn.classList.remove('text-gray-700', 'hover:bg-gray-100');
    listViewBtn.classList.remove('bg-blue-600', 'text-white');
    listViewBtn.classList.add('text-gray-700', 'hover:bg-gray-100');
});

listViewBtn.addEventListener('click', () => {
    listView.classList.remove('hidden');
    gridView.classList.add('hidden');
    listViewBtn.classList.add('bg-blue-600', 'text-white');
    listViewBtn.classList.remove('text-gray-700', 'hover:bg-gray-100');
    gridViewBtn.classList.remove('bg-blue-600', 'text-white');
    gridViewBtn.classList.add('text-gray-700', 'hover:bg-gray-100');
});

// 周切换
prevWeekBtn.addEventListener('click', () => {
    if (currentWeek > 1) {
        currentWeek--;
        updateWeekDisplay();
        // 这里可以添加周切换动画效果
    }
});

nextWeekBtn.addEventListener('click', () => {
    if (currentWeek < 20) {
        currentWeek++;
        updateWeekDisplay();
        // 这里可以添加周切换动画效果
    }
});

// 课程点击事件 - 使用事件委托
gridView.addEventListener('click', (e) => {
    const courseCard = e.target.closest('[data-course-id]');
    if (courseCard) {
        const courseId = parseInt(courseCard.getAttribute('data-course-id'));
        showCourseDetails(courseId);
    }
});

listView.addEventListener('click', (e) => {
    const courseCard = e.target.closest('.bg-white.rounded-lg.shadow-sm');
    if (courseCard) {
        // 从课程卡片中获取课程ID，这里简化处理，实际应从数据中获取
        const courseName = courseCard.querySelector('h4').textContent;
        const course = courses.find(c => c.name === courseName);
        if (course) {
            showCourseDetails(course.id);
        }
    }
});

// 显示课程详情
function showCourseDetails(courseId) {
    const course = courses.find(c => c.id === courseId);
    if (!course) return;
    
    // 填充模态框数据
    document.getElementById('modalCourseTitle').textContent = course.name;
    document.getElementById('modalCourseCode').textContent = course.code;
    document.getElementById('modalCourseName').textContent = course.name;
    document.getElementById('modalCourseTeacher').textContent = course.teacher;
    document.getElementById('modalCourseType').textContent = course.type + '课';
    document.getElementById('modalCourseCredits').textContent = course.credits;
    document.getElementById('modalCourseTime').textContent = course.time;
    document.getElementById('modalCourseLocation').textContent = course.location;
    document.getElementById('modalCourseWeeks').textContent = course.weeks;
    document.getElementById('modalCourseDescription').textContent = course.description;
    
    // 填充教学大纲
    const syllabusList = document.getElementById('modalCourseSyllabus');
    syllabusList.innerHTML = '';
    course.syllabus.forEach(item => {
        const li = document.createElement('li');
        li.textContent = item;
        syllabusList.appendChild(li);
    });
    
    // 显示模态框
    courseModal.classList.remove('hidden');
    document.body.classList.add('overflow-hidden'); // 防止背景滚动
    
    // 添加动画效果
    const modalContent = courseModal.querySelector('.bg-white');
    modalContent.classList.add('animate-fade-in');
}

// 关闭模态框
function closeModal() {
    courseModal.classList.add('hidden');
    document.body.classList.remove('overflow-hidden');
}

closeModalBtn.addEventListener('click', closeModal);
cancelModalBtn.addEventListener('click', closeModal);

// 点击模态框背景关闭
courseModal.addEventListener('click', (e) => {
    if (e.target === courseModal) {
        closeModal();
    }
});

// 初始化
updateWeekDisplay();
    